package com.qdlc.p2p.dal.checkrule;

import org.springframework.stereotype.Service;

import com.qdlc.p2p.common.exception.UserException;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.dto.User;

/**
 * 规则约束-平台安全性
 *
 * @author xx
 * @version 2.0
 * @since 2014年1月23日
 */
@Service
public class SafetyRuleCheck extends RuleCheck {
    /** 后台安全性 */
    public SafetyAdmin admin;

    /** 前台安全性 */
    public SafetyFront front;

    public SafetyRuleCheck() {
        super();
    }

    @Override
    public boolean checkRule() {
        return false;
    }

    public static class SafetyAdmin {
        /** 状态，1：启用，0：关闭 */
        public int status;

        /** 用户名-禁用关键字 */
        public String unallowed_username;

        public void setStatus(int status) {
            this.status = status;
        }

        public void setUnallowed_username(String unallowed_username) {
            this.unallowed_username = unallowed_username;
        }
    }

    public static class SafetyFront {
        /** 状态，1：启用，0：关闭 */
        public int status;

        /** 用户名-禁用关键字 */
        public String unallowed_username;

        public void setStatus(int status) {
            this.status = status;
        }

        public void setUnallowed_username(String unallowed_username) {
            this.unallowed_username = unallowed_username;
        }
    }

    public static class SafetyFrontPwdSecret {
        public String password_token;
        public int phone_sms;

        public void setPassword_token(String password_token) {
            this.password_token = password_token;
        }

        public void setPhone_sms(int phone_sms) {
            this.phone_sms = phone_sms;
        }

    }

    public void setAdmin(SafetyAdmin admin) {
        this.admin = admin;
    }

    public void setFront(SafetyFront front) {
        this.front = front;
    }

    /**
     * 数据校验方法
     */
    public int hasFontUnallowedUsername(User user) {
        if (user == null || StringUtil.isBlank(user.getUserName())) {
            throw new UserException("请填写用户名!", 1);
        }
        if (front != null && front.status == 1) {
            String fuu = front.unallowed_username;
            if (StringUtil.isNotBlank(fuu) && fuu.contains(user.getUserName())) {
                throw new UserException("请勿使用含有敏感词汇的用户名：" + fuu, 1);
            }
        }
        return 1;
    }

    public int hasFontUnallowedUsername(String userName) {
        if (StringUtil.isBlank(userName)) {
            throw new UserException("请填写用户名!", 1);
        }
        if (front != null && front.status == 1) {
            String fuu = front.unallowed_username;
            if (StringUtil.isNotBlank(fuu) && fuu.contains(userName)) {
                throw new UserException("请勿使用含有敏感词汇的用户名：" + fuu, 1);
            }
        }
        return 1;
    }
}
